package zhou.teamwork12;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class Q1_UserController {
	
	//---------登陆操作--------
	public boolean login(User u){
		boolean result=false;
		BaseDao dao=new BaseDao();
		Connection con=dao.getCon();				
    	String sql="select pwd from user where userName='"+u.getUserName()+"'";
    	
    	  try{		
    	  PreparedStatement ps=con.prepareStatement(sql);  	 
    	  ResultSet rs=ps.executeQuery();    	     	
    	       	  
    	  while(rs.next()){
				if(rs.getString("pwd").equals(u.getPwd())){
					System.out.println("登陆成功");
					result=true;
				}else{
					System.out.println("用户名或密码输入错误！！！");
				}
			} 			  
   	  if(rs!=null){
    		  rs.close();
    	  }
   	  if(ps!=null){
    		ps.close();    		   		
    	  }
    	  if(con!=null){
    		con.close();    		   		
   	  }		
    			
          }catch(SQLException e){
    	     e.printStackTrace();
    	}
    	return true;
    }
    

	//-----------注册用户-------------
    public boolean register(User u){
	BaseDao dao=new BaseDao();
	Connection con=dao.getCon();
	String sql1="select username from user where username='"+u.getUserName()+"'";
	String sql2="insert into user (userName,pwd) value (?,?)";
	boolean result=false;
	  try{		
	  PreparedStatement ps1=con.prepareStatement(sql1);
	  ResultSet rs=ps1.executeQuery();
		while(rs.next())
		{
			System.out.println("用户名存在！！");
			if(rs!=null){
				rs.close();
			}
			if(ps1!=null){
				ps1.close();
			}
			return result;
	}		
	  PreparedStatement ps2=con.prepareStatement(sql2);
	  ps2.setString(1, u.getUserName());
	  ps2.setString(2, u.getPwd());
	  ps2.executeUpdate();
	  if(ps2!=null){
		ps2.close();    		   		
	  }
	  if(rs!=null){
			rs.close();
		}
		if(ps1!=null){
			ps1.close();
		}
	  if(con!=null){
		con.close();    		   		
	  }		
			
      }catch(SQLException e){
	     e.printStackTrace();
	}
	return true;
}
 
  //---------用户申请开店--------  
    public boolean openShop(User u){
    	BaseDao dao=new BaseDao();
    	Connection con=dao.getCon();
    	String sql1="select * from shop where name='"+u.getShop().getName()+"'";
    	String sql2="insert into shop (shopName,mark) value (?,?)";
    	boolean result=false;
    	try{    		
    	PreparedStatement ps1=con.prepareStatement(sql1);
    	ResultSet rs1=ps1.executeQuery();
    	
    	while(rs1.next()){   		
    		System.out.println("该商店名称已存在，请申请其他商店名称");
    		if(rs1!=null){
				rs1.close();
			}
			if(ps1!=null){
				ps1.close();
			}
			return result;
    		  		
    	}   	
			PreparedStatement ps2=con.prepareStatement(sql2);   	
	    	ps2.setString(1, u.getShop().getName());
	    	ps2.setString(2, u.getShop().getMark());
	    	ps2.executeUpdate();
	    	System.out.println("申请成功");
	    	if(ps2!=null){
	    		ps2.close();    		   		
	    	  }		
    	
    	if(rs1!=null){
    		rs1.close();
    	}
   	
    	if(ps1!=null){
    		ps1.close();    		   		
    	  }
    	
    	if(con!=null){
    		con.close();    		   		
    	  }		
    			
        }catch(SQLException e){
    	     e.printStackTrace();
    	}
    	return true;
    }

}
